package com.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.mapper.CheckGroupMapper;
import com.health.pojo.CheckGroup;
import com.health.service.CheckGroupService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper,CheckGroup> implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupMapper.add(checkGroup);
        Integer checkGroupId=checkGroup.getId();
        this.setCheckGroupAndCheckItem(checkGroupId,checkitemIds);
    }

    @Override
    public Page<CheckGroup> pageQuery(Integer pagenum, Integer pagesize, String pageQuery) {
        QueryWrapper<CheckGroup> wrapper=new QueryWrapper<>();
        Page<CheckGroup> page=new Page<>(pagenum,pagesize);
        if (pageQuery!=null&&pageQuery.length()>0){
            wrapper.like("code",pageQuery).or().like("name",pageQuery).or().like("helpCode",pageQuery);
            page=this.page(page,wrapper);
        }else {
            page=this.page(page,null);
        }
        return page;
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupMapper.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupMapper.updateById(checkGroup);
        checkGroupMapper.deleteAssocication(checkGroup.getId());
        Integer checkGroupId=checkGroup.getId();
        this.setCheckGroupAndCheckItem(checkGroupId,checkitemIds);
    }

    public void setCheckGroupAndCheckItem(Integer checkGroupId,Integer[] checkitemIds){
        if(checkitemIds!=null&&checkitemIds.length>0){
            for(Integer checkitemId:checkitemIds){
                Map<String,Integer> map=new HashMap<>();
                map.put("checkgroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                checkGroupMapper.setCheckGroupAndCheckItem(map);
            }
        }
    }
}
